<?php
/**
 * Created by PhpStorm.
 * User: tony
 * Date: 2018/6/4
 * Time: 11:20
 */

namespace app\modules\v1\controllers\parent;

use app\filters\ApiFilter;
use app\models\ActionLog;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\Cors;
use yii\rest\Controller;
use yii\helpers\ArrayHelper;

class ParentController extends Controller
{
    public $serializer = [
        'class' => 'yii\rest\Serializer',
        // 配置用户自定义输出序列化行为
        'collectionEnvelope' => '_items',
    ];

    public function behaviors()
    {
        $behaviors = ArrayHelper::merge(
            [
                [
                    'class' => Cors::class,
                    'cors'=>[
                        // restrict access to
                        'Origin' => ALLOW_ORIGIN,
                        'Access-Control-Request-Headers' => ['*'],
                    ]
                ],
            ],
            parent::behaviors()
        );
        $behaviors['authenticator'] = ['class' => HttpBearerAuth::class];
        $behaviors['apiFilter'] = ['class' => ApiFilter::class];
        return $behaviors;
    }

    // 检查要修改的记录的用户ID是不是自己的ID
    public function isMine($user_id){
        if(\Yii::$app->user->id === $user_id)
            return true;
        return false;
    }

    public function ActionIndex()
    {
        return 1231;
    }
//    // 建新，修改，删除日志
//    public function afterAction($action, $result)
//    {
//        if(in_array($action->id,['update','delete','create']) && isset($result['bCode'])){
//            if($result['bCode'] === 101 ){
//                $actionLog = new ActionLog();
//                $actionLog->user_id = \Yii::$app->user->id;
//                $actionLog->created_at = time();
//                $actionLog->controller = $this->id;
//                $actionLog->action = $action->id;
//                $actionLog->data = serialize($result['bData']->toArray());
//                $actionLog->save();
//            }
//        }
//        return parent::afterAction($action, $result); // TODO: Change the autogenerated stub
//    }
}